//
//  MPEpisode.m
//  MissPaparazzi
//
//  Created by Alexey Aleshkov on 09.09.12.
//  Copyright (c) 2012 DzeTech. All rights reserved.
//


#import "MPEpisode.h"
#import "MPStore.h"


@implementation MPEpisode

@dynamic productID;
@dynamic name;
@dynamic fullText;
@dynamic free;
@dynamic size;
@dynamic date;
@dynamic imageDownloaded;

@dynamic price;
@dynamic wasPurchased;
@dynamic downloaded;

@synthesize downloadState = _downloadState;
@synthesize totalBytes = _totalBytes;
@synthesize readBytes = _readBytes;

- (void)awakeFromFetch
{
	[super awakeFromFetch];
	self.totalBytes = 1;
	self.readBytes = 0;
}

- (void)awakeFromInsert
{
	[super awakeFromInsert];
	self.totalBytes = 1;
	self.readBytes = 0;
}

- (void)dealloc
{
	[_thumbnailImage release];
	[super dealloc];
}

- (NSString *)coreDataDescription
{
	NSString *result = [NSString stringWithFormat:@"<%@: %p> (productID: %@; name: %@; fullText: %@; free: %@; size: %@; date: %@)", [self class], self, self.productID, self.name, self.fullText, self.free, self.size, self.date];
	return result;
}

+ (NSString *)coreDataDescriptionForArray:(NSArray *)array
{
	NSString *result;
	
	NSMutableString *s = [[NSMutableString alloc] init];
	
	[s appendString:@"(\n"];
	
	SEL sel = @selector(coreDataDescription);
	if ([array count]) {
		for (NSInteger i = 0; i < [array count] - 1; i++) {
			id item = [array objectAtIndex:i];
			if ([item respondsToSelector:sel]) {
				NSString *desc = [item coreDataDescription];
				[s appendFormat:@"\t(%@),\n", desc];
			}
		}
		id item = [array objectAtIndex:[array count] - 1];
		if ([item respondsToSelector:sel]) {
			NSString *desc = [item coreDataDescription];
			[s appendFormat:@"\t(%@)\n", desc];
		}
	}
	
	[s appendString:@")\n"];
	
	result = [[s copy] autorelease];
	[s release];
	
	return result;
}

//- (MPEpisodeDownloadState)downloadState
//{
//	MPEpisodeDownloadState result = _downloadState;
//	return result;
//}

- (UIImage *)thumbnailImage
{
	if (_thumbnailImage) {
		return _thumbnailImage;
	}

	dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
		__block UIImage *result = nil;
		BOOL imageExists = [[MPStore sharedInstance] isImageExists:self];
		if (imageExists) {
			NSString *imagePath = [[MPStore sharedInstance] imagePathForItem:self];
			result = [UIImage imageWithContentsOfFile:imagePath];
			[result retain];
			
			dispatch_async(dispatch_get_main_queue(), ^{
				[self willChangeValueForKey:@"thumbnailImage"];
				_thumbnailImage = result;
				[self didChangeValueForKey:@"thumbnailImage"];
			});
		}
	});

	return nil;
}

@end



@implementation ImageToDataTransformer

+ (BOOL)allowsReverseTransformation
{
	return YES;
}

+ (Class)transformedValueClass
{
	return [NSData class];
}

- (id)transformedValue:(id)value
{
	NSData *result = (value == nil) ? nil : UIImagePNGRepresentation(value);
	return result;
}

- (id)reverseTransformedValue:(id)value
{
	UIImage *result = (value == nil) ? nil : [UIImage imageWithData:value];
	return result;
}

@end
